package com.yanqu.road.server.manger.activity.jumppot;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.autodata.hotpotjump.UserHotpotJumpGameData;
import com.yanqu.road.entity.autodata.hotpotjump.config.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.hotpotjump.HotpotJumpConfig;
import com.yanqu.road.logic.activity.hotpotjump.game.HotpotJumpGameLogic;
import com.yanqu.road.logic.pb.HotpotJumpPb;
import com.yanqu.road.pb.activity.HotpotJumpProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.jumppot.HotpotJumpModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class HotpotJumpMgr extends TempMgr {
    private static HotpotJumpConfig config = null;

    /**
     * 引导数据
     */
    private static List<HotpotJumpProto.HotpotJumpGuideMsg> guideMsgList = new ArrayList<>();
    /**
     * 引导结束的盘面
     */
    private static UserHotpotJumpGameData gameData = null;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivity() {
        getLogger().info("reload hotpot jump activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.HotpotJump.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no hotpot jump Activity in show time");
            config = null;
        } else {
            ActivityInfo tempActivityInfo = openActivityInfoList.get(0);
            List<Integer> activityIdList = new ArrayList<>();
            int activityId = tempActivityInfo.getActivityId();
            activityIdList.add(activityId);
            Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityId);
            HotpotJumpConfig tempConfig = new HotpotJumpConfig(tempActivityInfo, activityConfigMap);

            config = tempConfig;

            Class<HotpotJumpGameLogic> hotpotJumpGameLogicClass = HotpotJumpGameLogic.class;
            initGuideMsg();
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(HotpotJumpModule.class).initSystemOpen();
        }

        getLogger().info("reload hotpot jump activity end");
    }

    public static HotpotJumpProto.HotpotJumpConfigSyncMsg.Builder parseConfigMsg(String language) {
        if (config == null) {
            return null;
        }

        HotpotJumpProto.HotpotJumpConfigSyncMsg.Builder configMsg = HotpotJumpProto.HotpotJumpConfigSyncMsg.newBuilder();
        configMsg.setActivityId(config.getActivityId());

        for (HotpotJumpPotConfig potConfig : config.getPotConfigMap().values()) {
            HotpotJumpProto.HotpotJumpPotConfigTemp.Builder builder = HotpotJumpProto.HotpotJumpPotConfigTemp.newBuilder();
            builder.setId(potConfig.getId());
            builder.setName(ServerLanguageMgr.getContent(potConfig.getName(), language));
            builder.setTalkOne(ServerLanguageMgr.getContent(potConfig.getTalkOne(), language));
            builder.setTalkTwo(ServerLanguageMgr.getContent(potConfig.getTalkTwo(), language));
            configMsg.addPotConfig(builder);
        }

        for (HotpotJumpNumConfig numConfig : config.getNumConfigList()) {
            HotpotJumpProto.HotpotJumpNumConfigTemp.Builder builder = HotpotJumpProto.HotpotJumpNumConfigTemp.newBuilder();
            builder.setId(numConfig.getId());
            builder.setRound(numConfig.getRound());
            builder.setStableNum(numConfig.getStableNum());
            builder.setMaxNum(numConfig.getMaxNum());
            builder.setMinNum(numConfig.getMinNum());
            configMsg.addNumConfig(builder);
        }

        for (HotpotJumpModelConfig modelConfig : config.getModelConfigMap().values()) {
            HotpotJumpProto.HotpotJumpModelConfigTemp.Builder builder = HotpotJumpProto.HotpotJumpModelConfigTemp.newBuilder();
            builder.setId(modelConfig.getId());
            builder.setType(modelConfig.getType());
            builder.setHp(modelConfig.getHp());
            builder.setAtk(modelConfig.getAtk());
            builder.setAtkRange(modelConfig.getAtkRange());
            builder.setActionNum(modelConfig.getActionNum());
            builder.setCriticalRate(modelConfig.getCriticalRate());
            builder.setName(ServerLanguageMgr.getContent(modelConfig.getName(), language));
            builder.setDesc(ServerLanguageMgr.getContent(modelConfig.getDesc(), language));
            builder.setScore(modelConfig.getScore());
            builder.setNormalSkill(modelConfig.getNormalSkill());
            builder.setSpecialSkill(modelConfig.getSpecialSkill());
            builder.setQuality(modelConfig.getQuality());
            configMsg.addModelConfig(builder);
        }

        for (HotpotJumpLevelConfig levelConfig : config.getLevelConfigMap().values()) {
            HotpotJumpProto.HotpotJumpLevelConfigTemp.Builder builder = HotpotJumpProto.HotpotJumpLevelConfigTemp.newBuilder();
            builder.setId(levelConfig.getId());
            builder.setLevel(levelConfig.getLevel());
            builder.setType(levelConfig.getType());
            builder.setHpIncrease(levelConfig.getHpIncrease());
            builder.setAtkIncrease(levelConfig.getAtkIncrease());
            builder.setExp(levelConfig.getExp());
            configMsg.addLevelConfig(builder);
        }

        for (HotpotJumpMissionConfig missionConfig : config.getMissionConfigs()) {
            HotpotJumpProto.HotpotJumpMissionConfigTemp.Builder builder = HotpotJumpProto.HotpotJumpMissionConfigTemp.newBuilder();
            builder.setId(missionConfig.getId());
            builder.setLevel(missionConfig.getLevel());
            builder.setParam(missionConfig.getParam());
            configMsg.addMissionConfig(builder);
        }

        for (HotpotJumpSkillConfig skillConfig : config.getSkillConfigMap().values()) {
            HotpotJumpProto.HotpotJumpSkillConfigTemp.Builder builder = HotpotJumpProto.HotpotJumpSkillConfigTemp.newBuilder();
            builder.setId(skillConfig.getId());
            builder.setSort(skillConfig.getSort());
            builder.setChooseType(skillConfig.getChooseType());
            builder.setTriggerTiming(skillConfig.getTriggerTiming());
            builder.setEffect(skillConfig.getEffect());
            builder.setRange(skillConfig.getRange());
            builder.setName(ServerLanguageMgr.getContent(skillConfig.getName(), language));
            builder.setDesc(ServerLanguageMgr.getContent(skillConfig.getDesc(), language));
            builder.setWeight(skillConfig.getWeight());
            builder.setMaxLevel(skillConfig.getMaxLevel());
            configMsg.addSkillConfig(builder);
        }

        return configMsg;
    }

    public static HotpotJumpConfig getConfig() {
        return config;
    }

    public static HotpotJumpProto.HotpotJumpConfigSyncMsg.Builder getConfigMsg(String language) {
        if (config == null) {
            return null;
        }

        HotpotJumpProto.HotpotJumpConfigSyncMsg.Builder configMsg = config.getConfigMsg(language);
        if (configMsg == null) {
            synchronized (config) {
                configMsg = config.getConfigMsg(language);
                if (configMsg == null) {
                    configMsg = parseConfigMsg(language);
                    config.setConfigMsg(language, configMsg);
                }
            }
        }
        return configMsg;
    }

    public static int checkInShowTime(int activityId) {
        if (config == null || config.getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return ActivityMgr.activityInShowTime(config.getActivityInfo()) ? 0 : GameErrorCode.E_ACTIVITY_OVER_TIME;
    }

    public static int checkInTime(int activityId) {
        if (config == null || config.getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return ActivityMgr.activityInTime(config.getActivityInfo()) ? 0 : GameErrorCode.E_ACTIVITY_OVER_TIME;
    }

    public static void initGuideMsg() {
        if (config == null) {
            return;
        }

        List<HotpotJumpProto.HotpotJumpGuideMsg> tempGuideMsgList = new ArrayList<>();
        HotpotJumpGameLogic logic = new HotpotJumpGameLogic(config, null);
        {
            logic.initGuideGame();

            HotpotJumpProto.HotpotJumpGuideMsg.Builder guideMsg = HotpotJumpProto.HotpotJumpGuideMsg.newBuilder();
            guideMsg.setGame(HotpotJumpPb.parseGameDataBuilder(logic.getGameData()));
            tempGuideMsgList.add(guideMsg.build());
        }

        {//第一步
            logic = new HotpotJumpGameLogic(config, logic.getGameData());
            HotpotJumpProto.HotpotJumpGuideMsg.Builder guideMsg = HotpotJumpProto.HotpotJumpGuideMsg.newBuilder();
            List<HotpotJumpProto.HotpotJumpGridTemp> moveList = new ArrayList<>();
            List<List<Integer>> guideMove = config.getGuideMove(logic.getGameData().getRound());
            for (List<Integer> list : guideMove) {
                int x = list.get(0);
                int y = list.get(1);
                HotpotJumpProto.HotpotJumpGridTemp.Builder moveTemp = HotpotJumpProto.HotpotJumpGridTemp.newBuilder();
                moveTemp.setX(x);
                moveTemp.setY(y);
                moveList.add(moveTemp.build());
            }
            int ret = logic.jumpByGuide(moveList);

            guideMsg.setGame(HotpotJumpPb.parseGameDataBuilder(logic.getGameData()));
            guideMsg.addAllShows(HotpotJumpPb.parseShowListBuilder(logic.getShowList()));
            tempGuideMsgList.add(guideMsg.build());
        }

        {//第二步
            logic = new HotpotJumpGameLogic(config, logic.getGameData());
            HotpotJumpProto.HotpotJumpGuideMsg.Builder guideMsg = HotpotJumpProto.HotpotJumpGuideMsg.newBuilder();
            List<HotpotJumpProto.HotpotJumpGridTemp> moveList = new ArrayList<>();
            List<List<Integer>> guideMove = config.getGuideMove(logic.getGameData().getRound());
            for (List<Integer> list : guideMove) {
                int x = list.get(0);
                int y = list.get(1);
                HotpotJumpProto.HotpotJumpGridTemp.Builder moveTemp = HotpotJumpProto.HotpotJumpGridTemp.newBuilder();
                moveTemp.setX(x);
                moveTemp.setY(y);
                moveList.add(moveTemp.build());
            }
            int ret = logic.jumpByGuide(moveList);

            guideMsg.setGame(HotpotJumpPb.parseGameDataBuilder(logic.getGameData()));
            guideMsg.addAllShows(HotpotJumpPb.parseShowListBuilder(logic.getShowList()));
            tempGuideMsgList.add(guideMsg.build());
        }

        {//第三步选技能
            logic = new HotpotJumpGameLogic(config, logic.getGameData());
            Integer skillId = config.getGuideSkillPool().get(config.getGuideSelectSkillIdx() - 1);
            logic.skillChooseByGuide(skillId);
            HotpotJumpProto.HotpotJumpGuideMsg.Builder guideMsg = HotpotJumpProto.HotpotJumpGuideMsg.newBuilder();
            guideMsg.setGame(HotpotJumpPb.parseGameDataBuilder(logic.getGameData()));
            guideMsg.addAllShows(HotpotJumpPb.parseShowListBuilder(logic.getShowList()));
            tempGuideMsgList.add(guideMsg.build());
        }

        {//第四步
            logic = new HotpotJumpGameLogic(config, logic.getGameData());
            HotpotJumpProto.HotpotJumpGuideMsg.Builder guideMsg = HotpotJumpProto.HotpotJumpGuideMsg.newBuilder();
            List<HotpotJumpProto.HotpotJumpGridTemp> moveList = new ArrayList<>();
            List<List<Integer>> guideMove = config.getGuideMove(logic.getGameData().getRound());
            for (List<Integer> list : guideMove) {
                int x = list.get(0);
                int y = list.get(1);
                HotpotJumpProto.HotpotJumpGridTemp.Builder moveTemp = HotpotJumpProto.HotpotJumpGridTemp.newBuilder();
                moveTemp.setX(x);
                moveTemp.setY(y);
                moveList.add(moveTemp.build());
            }
            int ret = logic.jumpByGuide(moveList);

            guideMsg.setGame(HotpotJumpPb.parseGameDataBuilder(logic.getGameData()));
            guideMsg.addAllShows(HotpotJumpPb.parseShowListBuilder(logic.getShowList()));
            tempGuideMsgList.add(guideMsg.build());
        }

        guideMsgList = tempGuideMsgList;
        gameData = logic.getGameData();
    }

    public static List<HotpotJumpProto.HotpotJumpGuideMsg> getGuideMsgList() {
        return guideMsgList;
    }

    public static UserHotpotJumpGameData getGameData() {
        return gameData;
    }
}
